/**
 * @description lazyMan 支持sleep以及链式调用
 *  
*/

class lazyMan{
    private name: string = '';
    private tasks: Function[] = [];

    protected next(){
        const task = this.tasks.shift();
        if(task){
            task();
        }
    }

    constructor(name: string){
        this.name = name;

        setTimeout(()=>{
            this.next();
        });        
    }

    eat(food: string){
        const task = () => {
            console.log(`${this.name} 吃了 ${food}`);
            this.next();
        }
        this.tasks.push(task);
        return this;
    }

    sleep(seconds: number){
        const task = () => {
            console.log('开始睡了');
            setTimeout(() => {
                console.log(`${this.name} 睡了${seconds} 秒，可以执行下一个任务了`);
                this.next();
            }, seconds * 1000);
        }
        this.tasks.push(task);
        return this;
    }
}

const me = new lazyMan('jeff');
me.eat('apple').eat('orange').sleep(3).eat('banner');